#include "Sort.h"

int My_Sort::BinSearch(int* a, int n, int p)
{
	int left = 0;
	int right = n - 1;
	while (left < right)
	{
		int mid = (left + right) / 2;
		if (a[mid] == p)
		{
			return mid;
		}
		else if (a[mid] > p)
		{
			right = mid - 1;
		}
		else
		{
			left = mid + 1;
		}
	}
	return -1;
}

void My_Sort::InsertSort(int* a, int n)
{
	for (int i = 0; i < n - 1; i++)
	{
		int end = i;
		int key = a[end + 1];
		while (end >= 0)
		{
			if (a[end] > key)
			{
				a[end + 1] = a[end];
				end--;
			}
			else
			{
				break;
			}
		}
		a[end + 1] = key;
	}
}

void My_Sort::ShellSort(int* a, int n)
{
	int p = n;
	while (p)
	{
		p /= 2;
		for (int i = 0; i < n - p; i++)
		{
			int end = i;
			int key = a[end + p];
			while (end >= 0)
			{
				if (a[end] > key)
				{
					a[end + p] = a[end];
					end -= p;
				}
				else
				{
					break;
				}
			}
			a[end + p] = key;
		}
	}
}

void My_Sort::BubbleSort(int* a, int n)
{
	for (int i = 0; i < n - 1; i++)
	{
		int k = 0;
		for (int z = 0; z < n - i - 1; z++)
		{
			if (a[z] > a[z + 1])
			{
				swap(a[z], a[z + 1]);
				k = 1;
			}
		}
		if (k == 0)
		{
			break;
		}
	}
}

void My_Sort::SelectSort(int* a, int n)
{
	int i = 0;
	while (i < (n / 2))
	{
		int max = i;
		int min = n - i - 1;
		for (int p = i; p <= n - i - 1; p++)
		{
			if (a[p] > a[max])
			{
				max = p;
			}
			if (a[p] < a[min])
			{
				min = p;
			}
		}
		swap(a[min], a[i]);
		if (max == i)
		{
			max = min;
		}
		swap(a[max], a[n - i - 1]);
		i++;
	}
}

void AdjustDwon(int* a, int n, int root)
{
	int parent = root;
	int child = parent * 2 + 1;
	while (child < n)
	{
		if (child + 1 < n && a[child] < a[child + 1])
		{
			child++;
		}
		if (a[parent] < a[child])
		{
			swap(a[parent], a[child]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}

void My_Sort::HeapSort(int* a, int n)
{
	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDwon(a, n, i);
	}
	int end = n - 1;
	while (end > 0)
	{
		swap(a[0], a[end]);
		AdjustDwon(a, end, 0);
		end--;
	}
}

int middle(int* a, int left, int right)
{
	int mid = (left + right) >> 1;
	if (a[left] < a[mid])
	{
		if (a[mid] < a[right])
		{
			return mid;
		}
		else if (a[left] > a[right])
		{
			return left;
		}
		else
		{
			return right;
		}
	}
	else
	{
		if (a[mid] > a[right])
		{
			return mid;
		}
		else if (a[left] < a[right])
		{
			return left;
		}
		else
		{
			return right;
		}
	}
}

void My_Sort::QuickSort(int* a, int left, int right)
{
	if (left >= right)
	{
		return;
	}
	int index = middle(a, left, right);
	swap(a[index], a[left]);
	int key = a[left];
	int prev = left;
	int begin = left;
	int end = right;
	while (begin < end)
	{
		while (begin < end && a[end] >= key)
		{
			end--;
		}
		a[prev] = a[end];
		prev = end;
		while (begin < end && a[begin] <= key)
		{
			begin++;
		}
		a[prev] = a[begin];
		prev = begin;
	}
	a[end] = key;
	QuickSort(a, left, end - 1);
	QuickSort(a, end + 1, right);
}

void _MergeSort(int* a, int* ret, int begin, int end)
{
	if (begin >= end)
	{
		return;
	}
	int mid = (begin + end) >> 1;
	_MergeSort(a, ret, begin, mid);
	_MergeSort(a, ret, mid + 1, end);
	int begin1 = begin;
	int end1 = mid;
	int begin2 = mid + 1;
	int end2 = end;
	int index = begin;
	while (begin1 <= end1 && begin2 <= end2)
	{
		if (a[begin1] < a[begin2])
		{
			ret[index++] = a[begin1++];
		}
		else
		{
			ret[index++] = a[begin2++];
		}
	}
	while (begin1 <= mid)
	{
		ret[index++] = a[begin1++];
	}
	while (begin2 <= end)
	{
		ret[index++] = a[begin2++];
	}
	memcpy(a + begin, ret + begin, sizeof(int) * (end - begin + 1));
}

void My_Sort::MergeSort(int* a, int n)
{
	int* ret = (int*)malloc(sizeof(int) * n);
	_MergeSort(a, ret, 0, n - 1);
	free(ret);
}

void My_Sort::Print(int* a, int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
}
